package com.linln.modules.system.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.linln.common.utils.UUIDGenerator;
import com.linln.modules.system.dao.BannersMapper;
import com.linln.modules.system.dao.CustomMapper;
import com.linln.modules.system.entity.Banners;
import com.linln.modules.system.entity.BeautyProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class BannerService {

    @Autowired
    private BannersMapper bannersMapper;

    @Autowired
    private CustomMapper customMapper;

    public int saveBanner(String name, String imgUrl, String imgLinks, int forwards, int kinds, String remark) {
        Banners banners = new Banners();
        Date date = new Date();
        // 0 外网
        banners.setId(UUIDGenerator.generate());
        banners.setNames(name).setImgUrl(imgUrl)
                .setCreateTime(date).setUpdateTime(date).setRemark(remark)
                .setDeleted(0).setStates(0).setDiffer(0);
        if (forwards == 0) {
            banners.setForwards(forwards).setKinds(2);
        } else {
            banners.setForwards(forwards).setKinds(kinds);
            if (kinds == 0) {
                if (imgLinks.contains("http://") || imgLinks.contains("https://")) {
                    banners.setImgLinks(imgLinks);
                } else {
                    banners.setImgLinks("http://"+imgLinks);
                }
            } else {
                banners.setImgLinks(imgLinks);
            }
        }
        int insert = bannersMapper.insert(banners);
        return insert;
    }

    public int saveHtml(String name, String imgLinks, String remark) {
        Banners banners = new Banners();
        Date date = new Date();
        banners.setId(UUIDGenerator.generate());
        banners.setNames(name).setImgLinks(imgLinks).setRemark(remark)
                .setCreateTime(date).setUpdateTime(date)
                .setDeleted(0).setStates(0).setDiffer(1);
        int insert = bannersMapper.insert(banners);
        return insert;
    }

    public int banPickImg(String bannerId, int state) {
        Banners banners = new Banners();
        banners.setId(bannerId).setStates(state).setUpdateTime(new Date());
        int update = bannersMapper.updateByPrimaryKeySelective(banners);
        return update;
    }

    public int delImg(String bannerId) {
        Banners banners = new Banners();
        banners.setId(bannerId).setDeleted(1)
                .setUpdateTime(new Date());
        int update = bannersMapper.updateByPrimaryKeySelective(banners);
        return update;
    }


    public int updateBanner(String banId, String names, String imgUrl, String imgLinks, int forwards, int kinds, String remark) {
        Banners banners = new Banners();
        banners.setId(banId).setNames(names).setRemark(remark)
                .setImgUrl(imgUrl).setUpdateTime(new Date());
        if (forwards == 0) {
            banners.setForwards(0).setKinds(2);
        } else {
            banners.setForwards(forwards).setKinds(kinds);
            if (kinds == 0) {
                if (imgLinks.contains("http://") || imgLinks.contains("https://")) {
                    banners.setImgLinks(imgLinks);
                } else {
                    banners.setImgLinks("http://"+imgLinks);
                }
            } else {
                banners.setImgLinks(imgLinks);
            }
        }
        int update = bannersMapper.updateByPrimaryKeySelective(banners);
        return update;
    }

    public Page<Banners> getBannersPager(Integer pageNum, Integer pageSize, String name) {
        //getBanners
        PageHelper.startPage(pageNum, pageSize, true);
        Page<Banners> page = customMapper.getBanners(name);
        return page;
    }

    public Banners getBannerById(String id) {
        Banners key = bannersMapper.selectByPrimaryKey(id);
        return key;
    }

    public int saveBannerOrder(String id, Integer sorts) {
        Banners banners = new Banners();
        banners.setId(id).setNumbers(sorts).setUpdateTime(new Date());
        int end = bannersMapper.updateByPrimaryKeySelective(banners);
        return end;
    }
}
